package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyRsZck;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyRsZckMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyRsZckService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审评查验-职称库
 * @Author: yuncheng
 * @Date: 2024-01-11
 * @Version: V1.0
 */
@Service
public class SpcyRsZckServiceImpl extends BaseServiceImpl<SpcyRsZckMapper, SpcyRsZck> implements ISpcyRsZckService {

    @Resource
    @Lazy
    private SpcyRsZckMapper spcyRsZckMapper;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public IPage<SpcyRsZck> selectPageList(SpcyRsZck query, PageVo pageVo, OrderItemVo orderItemVo) {
        LambdaQueryWrapper<SpcyRsZck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFjb()), SpcyRsZck::getFjb, query.getFjb());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzcxllx1()), SpcyRsZck::getFzcxllx1, query.getFzcxllx1());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzcxllx2()), SpcyRsZck::getFzcxllx2, query.getFzcxllx2());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzyjszg()), SpcyRsZck::getFzyjszg, query.getFzyjszg());

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByAsc(SpcyRsZck::getForder);
            queryWrapper.orderByAsc(SpcyRsZck::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        Page<SpcyRsZck> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        return spcyRsZckMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SpcyRsZck> selectList(SpcyRsZck query) {
        LambdaQueryWrapper<SpcyRsZck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFjb()), SpcyRsZck::getFjb, query.getFjb());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzcxllx1()), SpcyRsZck::getFzcxllx1, query.getFzcxllx1());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzcxllx2()), SpcyRsZck::getFzcxllx2, query.getFzcxllx2());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzyjszg()), SpcyRsZck::getFzyjszg, query.getFzyjszg());
        queryWrapper.orderByAsc(SpcyRsZck::getForder);
        queryWrapper.orderByAsc(SpcyRsZck::getCreateTime);
        return spcyRsZckMapper.selectList(queryWrapper);
    }

    @Override
    public SpcyRsZck addZc(SpcyRsZck bean) {
        bean.setId(GetUuIdUtils.ReplaceUuId());

        verifyZc(bean);

        int order = selectList(new SpcyRsZck()).size()+1;
        bean.setForder(order);

        bean.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setCreateTime(new Date());
        bean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setUpdateTime(new Date());
        bean.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());

        spcyRsZckMapper.insert(bean);

        String operContent = "新增信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_RS_ZCXLGL,operContent,bean.getId(), "添加职称", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    @Override
    public void updatwZc(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        String id = buffer.get("id");
        if (StringUtils.isEmpty(id)){
            throw new BusinessException("参数为空!");
        }
        SpcyRsZck updateBean = spcyRsZckMapper.selectById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        updateBean.setUpdateTime(new Date());

        verifyZc(updateBean);

        spcyRsZckMapper.updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_RS_ZCXLGL,operContent,id,"修改职称",SpcyConstant.LOGS_SYS_TYPE_XG);

    }

    @Override
    public void deleteZc(String id) {
        if (StringUtils.isEmpty(id)){
            throw new BusinessException("参数为空!");
        }
        SpcyRsZck bean = spcyRsZckMapper.selectById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配数据！");
        }
        spcyRsZckMapper.deleteById(id);

        String operContent = "删除信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_RS_ZCXLGL,operContent,bean.getId(), "删除职称", SpcyConstant.LOGS_SYS_TYPE_SC);

    }

    private void verifyZc(SpcyRsZck bean){
        if (StringUtils.isBlank(bean.getFzyjszg())){
            throw new BusinessException("专业技术资格为空！");
        }
        if (StringUtils.isBlank(bean.getFzcxllx1())){
            throw new BusinessException("职称系列类型1为空！");
        }
        if (StringUtils.isBlank(bean.getFzcxllx2())){
            throw new BusinessException("职称系列类型2为空！");
        }
        if (StringUtils.isBlank(bean.getFjb())){
            throw new BusinessException("级别为空！");
        }

        List<SpcyRsZck> list = spcyRsZckMapper.selectList(new LambdaQueryWrapper<SpcyRsZck>()
                .eq(SpcyRsZck::getFzyjszg, bean.getFzyjszg())
                .eq(SpcyRsZck::getFzcxllx1, bean.getFzcxllx1())
                .eq(SpcyRsZck::getFzcxllx2, bean.getFzcxllx2())
                .eq(SpcyRsZck::getFjb, bean.getFjb()));

        if (CollectionUtil.isNotEmpty(list)){
            throw new BusinessException("数据已经存在！");
        }

    }
}
